package org.example.operator;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import org.example.bean.WaterSensor;

public class FlatmapOperator {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        DataStreamSource<WaterSensor> source = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s2", 1L, 2),
                new WaterSensor("s1", 1L, 13)
        );

        source.flatMap((WaterSensor value, Collector<String> out) -> {
            if ("s1".equals(value.getType())) {
                out.collect(value.getWaterRecord().toString());
            } else {
                out.collect(value.getSensorRecordTimestamp().toString());
                out.collect( " aa " + value.getWaterRecord().toString());
            }
        }).returns(Types.STRING)
                .print();




        env.execute();
    }
}

